package com.li;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lirenchun
 */
public class TimeUtils {

    public static void main(String[] args) throws ParseException {
        System.out.println(getTimeLastMonth("2020-01"));
    }

    /**
     * 获取当前月的上一个月:年-月
     * @param monthStr
     * @return
     */
    public static String getTimeLastMonth(String monthStr){
        String lastMonthStr = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            Date month = df.parse(monthStr);
            Calendar calMonth = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calMonth.setTime(month);
            calMonth.add(Calendar.MONTH, -1);
            lastMonthStr = df.format(calMonth.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return lastMonthStr;
    }

    /**
     * 获取当前时刻的上一时刻：时:00
     *
     * @param timeStr 时:00
     * @return
     */
    public static String getTimeLastTime(String timeStr) {
        String timeStrResult = "";
        int timeNum = new Integer(timeStr.split(":")[0]);
        if (1 <= timeNum && timeNum <= 23) {
            timeNum--;
        } else if (timeNum == 0) {
            timeNum = 23;
        }
        if (timeNum < 10) {
            timeStrResult = "0" + timeNum + ":00";
        } else {
            timeStrResult = timeNum + ":00";
        }
        System.out.println(timeStrResult);
        return timeStrResult;
    }

    /**
     * 一天24小时传值
     *
     * @param startTime 开始时间：时：分
     * @param endTime   结束时间：时：分
     * @return
     */
    public static int countTwoTimeHasSecond(String startTime, String endTime) {
        String[] startTimeStr = startTime.split(":");
        String[] endTimeStr = endTime.split(":");
        int startHour = Integer.valueOf(startTimeStr[0]);
        int endHour = Integer.valueOf(endTimeStr[0]);
        int startMinute = Integer.valueOf(startTimeStr[1]);
        int endMinute = Integer.valueOf(endTimeStr[1]);
        //计算花费时间
        int countSecondTime = (endHour - startHour) * 3600 + (endMinute - startMinute) * 60;
        return countSecondTime;
    }


    /**
     * 判断时间是否在两个日期范围内
     *
     * @param firstTime    起始时间 MM-dd
     * @param secondTime   结束时间 MM-dd
     * @param compareMonth 被比较时间的月  MM
     * @param compareDay   被比较时间的天  dd
     */
    public static boolean TimeIsTwoTimeHorizon(String firstTime, String secondTime, Integer compareMonth, Integer compareDay) {
        boolean status = false;
        //时间范围起始月
        int startMonth = new Integer(firstTime.split("-")[0]);
        //时间范围起始天
        int startDay = new Integer(firstTime.split("-")[1]);
        //时间范围结束月
        int endMonth = new Integer(secondTime.split("-")[0]);
        //时间范围结束天
        int endDay = new Integer(secondTime.split("-")[1]);
        if (compareMonth > startMonth && compareMonth < endMonth) {
            status = true;
        } else if (startMonth == compareMonth) {
            //如果日期再大于等于起始日期
            if (compareDay >= startDay) {
                status = true;
            }
        } else if (endMonth == compareMonth) {
            //如果日期再小于等于起始日期
            if (compareDay <= endDay) {
                status = true;
            }
        }
        return status;
    }

    /**
     * 获取当前日期的周下标(周一开始，下标0开始)
     *
     * @param date 时间 yyyy-MM-dd
     */
    public static Integer getDayOfWeekToIndex(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK);
        int index = 0;
        switch (weekNum) {
            //周末
            case 1:
                //下标
                index = 6;
                break;
            //周1
            case 2:
                //下标
                index = 0;
                break;
            //周2
            case 3:
                index = 1;
                break;
            //周3
            case 4:
                index = 2;
                break;
            //周4
            case 5:
                index = 3;
                break;
            //周5
            case 6:
                index = 4;
                break;
            //周6
            case 7:
                index = 5;
                break;
        }
        return index;
    }


}
